package com.threeti.jiayusms.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.threeti.jiayusms.domain.model.legacy.Order;
import com.threeti.jiayusms.domain.service.legacy.OrderQueryService;
import com.threeti.jiayusms.framework.util.PagedListHandler;
import com.threeti.jiayusms.framework.web.PagedQueryControllerAdapter;
import com.threeti.jiayusms.web.form.OrderTemplateForm;

@Controller
public class OrderMainController {
	@Autowired
	  private OrderQueryService queryService;
	
	@RequestMapping(value="/orderMaintenanceMain.html")
	  public String enterMsgTemplateMain(HttpServletRequest request,Map modelMap) {
	    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
	    int[] index = pagedHelper.getIndex(request);
	    PagedListHandler<Order> pagedMsgTemplates = queryService.findAllOrdersByPage(index[0], index[1]);
	    pagedHelper.processPage(pagedMsgTemplates, request);

	    modelMap.put("orderlist", pagedMsgTemplates.getPageList());

	    return "ordermaintenance/orderMain";
	  }
	
	@RequestMapping(value="/exportAllOrders.html")
	  public void exportLogTemplate(HttpServletRequest request, @ModelAttribute OrderTemplateForm form, Map modelMap,HttpServletResponse response) throws BiffException, IOException, WriteException {

	  	List<Order> orderlist = queryService.findAllOrder();
	  	WritableWorkbook workbook = Workbook.createWorkbook(new File("D:/order.xls"));
	  	WritableSheet sheet = workbook.createSheet("Orders", 0);
	  	WritableFont font = new WritableFont(WritableFont.TIMES, 11);
	  	font.setColour(Colour.BLACK);
	  	WritableCellFormat cellFormat = new WritableCellFormat();
	  	cellFormat.setFont(font);
	  	cellFormat.setAlignment(Alignment.CENTRE);
	  	cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.GRAY_80);
	  	Label l1 = new Label(0, 0, "运单号", cellFormat);
	  	Label l2 = new Label(1, 0, "客户ID", cellFormat);
	  	Label l3 = new Label(2, 0, "货号", cellFormat);
	  	Label l4 = new Label(3, 0, "货名", cellFormat);
	  	Label l5 = new Label(4, 0, "货物数量", cellFormat);
	  	Label l6 = new Label(5, 0, "优先发送", cellFormat);
	  	
	  	sheet.addCell(l1);
	  	sheet.addCell(l2);
	  	sheet.addCell(l3);
	  	sheet.addCell(l4);
	  	sheet.addCell(l5);
	  	sheet.addCell(l6);
	  	
	  	for(int i=1; i<=orderlist.size(); i++) {
	  		Order order = orderlist.get(i-1);
	  		List list = new ArrayList();
	  		list.add(order.getOrderid());
	  		list.add(order.getClientid());
	  		list.add(order.getCargono());
	  		list.add(order.getCargoname());
	  		list.add(String.valueOf(order.getCargocount()));
	  		list.add(order.getOrderPriority());
	  		for(int k=0; k<list.size(); k++) {
	  			Label label = new Label(k, i, (String)list.get(k), cellFormat);
	  			sheet.addCell(label);
	  		}	  		
	  	}
	  	workbook.write();
  		workbook.close();
	  }
}
